﻿using ATM.Common;
using ATM.DAO;
using ATM.Entities;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ATM.BUS
{
    public class WithdrawBUS
    {
        public static int Withdraw(float amount, Account acc, Card card, int atmID)
        {
            int tmp1 = 1;
            if (amount % 50000==0)
            {
                if (amount <= acc.WithdrawLimit)
                {
                    if (amount <= acc.Balance)
                    {
                        acc.Balance -= amount;
                        AccountDAO.UpdateAccount(acc);
                        LogDAO.WriteLog(card, tmp1, atmID, amount,"Say something");
                        return 2;
                    }
                    if (amount > acc.Balance)
                    {
                        float tmp2 = amount - acc.Balance;
                        if(tmp2<=acc.OverDraftLimit)
                        {
                            acc.Balance = 0 - tmp2;
                            AccountDAO.UpdateAccount(acc);
                            LogDAO.WriteLog(card, tmp1, atmID, amount,"Negative");
                            return 3;
                        }
                        return 4;
                    }
                }
                return 1;
            }
            return 0;
        }
    }
}
